package cm.android.mytodoit.view;

import java.util.ArrayList;

import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Window;
import android.widget.ArrayAdapter;
import cm.android.mytodoit.R;
import cm.android.mytodoit.controller.Todo_SQLOpenHelper;
import cm.android.mytodoit.controller.listeners.BuyListViewLongClickListener;
import cm.android.mytodoit.model.BuyElement;

public class BuyListView extends ListActivity{
	
	private SQLiteDatabase database;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.buylistview);
	}

	@Override
	protected void onResume() {

		this.database = new Todo_SQLOpenHelper(this).getReadableDatabase();
		
		Cursor cursor = database.query(BuyElement.TABLE_NAME,
				null,
				null,
				null,
				null,
				null,
				null);
		ArrayList<BuyElement> buyElements = new ArrayList<BuyElement>();
		
		while (cursor.moveToNext()) {
			
			BuyElement buyElement = new BuyElement();
			buyElement.setId(cursor.getInt(cursor.getColumnIndex(BuyElement.TC_ID)));
			buyElement.setName(cursor.getString(cursor.getColumnIndex(BuyElement.TC_NAME)));
			buyElement.setQuantity(cursor.getString(cursor.getColumnIndex(BuyElement.TC_QUANTITY)));
			buyElement.setInfo(cursor.getString(cursor.getColumnIndex(BuyElement.TC_INFO)));
			
			buyElements.add(buyElement);
		}
		
		ArrayAdapter<BuyElement> adapter = new ArrayAdapter<BuyElement>(this, android.R.layout.simple_expandable_list_item_1, buyElements);
		
		this.getListView().setAdapter(adapter);
		this.getListView().setOnItemLongClickListener(new BuyListViewLongClickListener(this));
		
		
		super.onResume();
	}
	
	@Override
	protected void onPause() {

		database.close();
		super.onPause();
	}
}
